package backend.user.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import backend.paging.Paging;
import backend.user.manager.UserManager;
import com.opensymphony.xwork2.ActionSupport;
import core.address.model.BasicAddress;
import core.address.model.city.City;
import core.address.model.continent.Continent;
import core.address.model.country.Country;
import core.address.model.region.Region;
import core.address.model.state.State;
import core.profile.model.BasicCredential;
import core.profile.model.BasicProfile;
import core.role.model.BasicRoles;
import dbinit.InitCity;
import dbinit.InitContinent;
import dbinit.InitCountry;
import dbinit.InitRegion;
import dbinit.InitState;
import frontend.registration.controller.Encript;

public class UserController extends ActionSupport 
{
	private static final long serialVersionUID = 1L;
	private List<BasicProfile> users ;
	private BasicProfile user;
	private List<BasicRoles> rolenames ;
	private BasicRoles basicroles ;
	private BasicCredential basiccredential;
	private BasicAddress currentAddress;
	private BasicAddress permanentAddress;
	private City city;
	private Continent continent;
	private Country country;
	private State state;
	private Region region;
	private InitCity icity;
	private InitContinent icontinent;
	private InitCountry icountry;
	private InitState istate;
	private InitRegion iregion;
	private Long uuid ;
	private int pageSize ;
	private int p ;
	private List<String> pageHtml ;
	private int totalPage ;
	private UserManager um ;
	private int usertype;
	private Boolean status;
	private String title;
	private String firstname;
	private String lastname;
	private String emailId;
	private String gender;
	private String companyname;
	private int adminusertype;
	private String password;
	private String confirmPassword;
	private String ca_addressLine1;
	private String ca_addressLine2;
	private String ca_phone;
	private String ca_phonebusiness;
	private String ca_mobile;
	private String ca_postalcode;
	private String ca_cityName;
	private String ca_continent;
	private String ca_countryName;
	private String ca_region;
	private String ca_stateName;
	private String pa_addressLine1;
	private String pa_addressLine2;
	private String pa_phone;
	private String pa_phonebusiness;
	private String pa_mobile;
	private String pa_postalcode;
	private String pa_cityName;
	private String pa_continent;
	private String pa_countryName;
	private String pa_region;
	private String pa_stateName;
	private String roleUuid;
	
	
	public UserController()
	{
		um=new UserManager();
	}
	// Action methods
	public String execute() 
	{
		if(pageSize == 0)
		{
			this.setPageSize(10);
		}
		Paging<BasicProfile> paging = new Paging<BasicProfile>();
		int totalCount = um.getCount();
		if(adminusertype==4)
		{
			users=um.getAdminUser();
		}
		else 
		{
			int start = paging.getStart(this.pageSize,this.p);
			int end = paging.getEnd(this.pageSize,this.p);
			users = um.getUserList(start , end);
			this.setUsers(users);
		}
		this.setPageHtml(paging.getPageHtml(this.getP(), pageSize, totalCount));
				
		return SUCCESS ;
	}
	
	public String editUser()
	{
		this.setUser(um.getUserByUuid(this.getUuid()));
		this.basiccredential=um.getcredetialByprofileid(user.getUuid());
		
		rolenames=um.getRoles();
		return SUCCESS ;
	}
	@SuppressWarnings("static-access")
	public String updateUser()
	{
		user=um.getUserByUuid(this.getUuid());
		basiccredential=um.getcredetialByprofileid(user.getUuid());
		this.password=Encript.doEncript(password);
		this.confirmPassword=Encript.doEncript(confirmPassword);	
		if(null!=basiccredential)
		{
		basiccredential=um.updateCredential(password, confirmPassword, basiccredential.getUuid());
		}
		if(usertype==4)
		{
			
			StringTokenizer str = new StringTokenizer(this.roleUuid.toString(), ",");
			Set<BasicRoles> brs = new HashSet<BasicRoles>();
			user.setBasicRoles(null);
			while (str.hasMoreTokens()) {
				Long roleId=Long.parseLong(str.nextToken().toString().trim());
				BasicRoles br = um.getRoleByUuid(roleId);
				brs.add(br);
			} 
			user.setBasicRoles(brs);
			um.saveOrUpdate(user);
			user=um.updateAdminUser(this.getUsertype(),this.getFirstname(), this.getLastname(), this.getEmailId(), this.getUuid());
			
		}
		else 
		{
		user=um.updateUser(this.getUsertype(), this.getStatus(), this.getTitle(), this.getFirstname(), this.getLastname(), this.getEmailId(), this.getGender(), this.getCompanyname(), this.getUuid());
		
		this.city=icity.getCityByName(this.getCa_cityName());
		this.country=icountry.getCountryByName(this.getCa_countryName());
		this.continent=icontinent.getContinentByName(this.getCa_continent());
		this.state=istate.getStateByName(this.getCa_stateName());
		this.region=iregion.getRegionByName(this.getCa_region());
		currentAddress=um.getCurrentAddressByUuid(this.user.getCurrentAddress().getUuid());
		currentAddress=um.updateCurrentAddress(this.getCa_addressLine1(), this.getCa_addressLine2(), this.getCa_phone(), this.getCa_phonebusiness(), this.getCa_mobile(), this.getCa_postalcode(),this.city,this.country,this.state,this.continent,this.region, this.user.getCurrentAddress().getUuid());
		this.city=icity.getCityByName(this.getPa_cityName());
		this.country=icountry.getCountryByName(this.getPa_countryName());
		this.continent=icontinent.getContinentByName(this.getPa_continent());
		this.state=istate.getStateByName(this.getPa_stateName());
		this.region=iregion.getRegionByName(this.getPa_region());
		permanentAddress=um.getPermanentAddressByUuid(this.user.getPermanentAddress().getUuid());
		permanentAddress=um.updatePermanentAddress(this.getPa_addressLine1(), this.getPa_addressLine2(), this.getPa_phone(), this.getPa_phonebusiness(), this.getPa_mobile(), this.getPa_postalcode(),this.city,this.country,this.state,this.continent,this.region, this.user.getPermanentAddress().getUuid());
		editUser();
		}
		return SUCCESS ;
	}
	public List<BasicProfile> getUsers() {
		return users;
	}
	public void setUsers(List<BasicProfile> users) {
		this.users = users;
	}
	
	public BasicProfile getUser() {
		return user;
	}
	public void setUser(BasicProfile user) {
		this.user = user;
	}
	
	public List<BasicRoles> getRolenames() {
		return rolenames;
	}
	public void setRolenames(List<BasicRoles> rolenames) {
		this.rolenames = rolenames;
	}
	public BasicRoles getBasicroles() {
		return basicroles;
	}
	public void setBasicroles(BasicRoles basicroles) {
		this.basicroles = basicroles;
	}
	
	public City getCity() {
		return city;
	}
	public void setCity(City city) {
		this.city = city;
	}
	
	public Continent getContinent() {
		return continent;
	}
	public void setContinent(Continent continent) {
		this.continent = continent;
	}
	public Country getCountry() {
		return country;
	}
	public void setCountry(Country country) {
		this.country = country;
	}
	public State getState() {
		return state;
	}
	public void setState(State state) {
		this.state = state;
	}
	public Region getRegion() {
		return region;
	}
	public void setRegion(Region region) {
		this.region = region;
	}
	public Long getUuid() {
		return uuid;
	}
	public void setUuid(Long uuid) {
		this.uuid = uuid;
	}
	

	public int getUsertype() {
		return usertype;
	}
	public void setUsertype(int usertype) {
		this.usertype = usertype;
	}
	public Boolean getStatus() {
		return status;
	}
	public void setStatus(Boolean status) {
		this.status = status;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getFirstname() {
		return firstname;
	}
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
	public String getLastname() {
		return lastname;
	}
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}
	public String getEmailId() {
		return emailId;
	}
	public void setEmailId(String emailId) {
		this.emailId = emailId;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getCompanyname() {
		return companyname;
	}
	public void setCompanyname(String companyname) {
		this.companyname = companyname;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getConfirmPassword() {
		return confirmPassword;
	}
	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}
	public BasicAddress getCurrentAddress() {
		return currentAddress;
	}
	public void setCurrentAddress(BasicAddress currentAddress) {
		this.currentAddress = currentAddress;
	}
	
	public BasicAddress getPermanentAddress() {
		return permanentAddress;
	}
	public void setPermanentAddress(BasicAddress permanentAddress) {
		this.permanentAddress = permanentAddress;
	}
	public String getCa_addressLine1() {
		return ca_addressLine1;
	}
	public void setCa_addressLine1(String ca_addressLine1) {
		this.ca_addressLine1 = ca_addressLine1;
	}
	public String getCa_addressLine2() {
		return ca_addressLine2;
	}
	public void setCa_addressLine2(String ca_addressLine2) {
		this.ca_addressLine2 = ca_addressLine2;
	}
	public String getCa_phone() {
		return ca_phone;
	}
	public void setCa_phone(String ca_phone) {
		this.ca_phone = ca_phone;
	}
	public String getCa_phonebusiness() {
		return ca_phonebusiness;
	}
	public void setCa_phonebusiness(String ca_phonebusiness) {
		this.ca_phonebusiness = ca_phonebusiness;
	}
	public String getCa_mobile() {
		return ca_mobile;
	}
	public void setCa_mobile(String ca_mobile) {
		this.ca_mobile = ca_mobile;
	}
	public String getCa_postalcode() {
		return ca_postalcode;
	}
	public void setCa_postalcode(String ca_postalcode) {
		this.ca_postalcode = ca_postalcode;
	}
	
	public String getCa_cityName() {
		return ca_cityName;
	}
	public void setCa_cityName(String ca_cityName) {
		this.ca_cityName = ca_cityName;
	}
	public String getCa_continent() {
		return ca_continent;
	}
	public void setCa_continent(String ca_continent) {
		this.ca_continent = ca_continent;
	}
	public String getCa_countryName() {
		return ca_countryName;
	}
	public void setCa_countryName(String ca_countryName) {
		this.ca_countryName = ca_countryName;
	}
	public String getCa_region() {
		return ca_region;
	}
	public void setCa_region(String ca_region) {
		this.ca_region = ca_region;
	}
	public String getCa_stateName() {
		return ca_stateName;
	}
	public void setCa_stateName(String ca_stateName) {
		this.ca_stateName = ca_stateName;
	}
	public String getPa_addressLine1() {
		return pa_addressLine1;
	}
	public void setPa_addressLine1(String pa_addressLine1) {
		this.pa_addressLine1 = pa_addressLine1;
	}
	public String getPa_addressLine2() {
		return pa_addressLine2;
	}
	public void setPa_addressLine2(String pa_addressLine2) {
		this.pa_addressLine2 = pa_addressLine2;
	}
	public String getPa_phone() {
		return pa_phone;
	}
	public void setPa_phone(String pa_phone) {
		this.pa_phone = pa_phone;
	}
	public String getPa_phonebusiness() {
		return pa_phonebusiness;
	}
	public void setPa_phonebusiness(String pa_phonebusiness) {
		this.pa_phonebusiness = pa_phonebusiness;
	}
	public String getPa_mobile() {
		return pa_mobile;
	}
	public void setPa_mobile(String pa_mobile) {
		this.pa_mobile = pa_mobile;
	}
	public String getPa_postalcode() {
		return pa_postalcode;
	}
	public void setPa_postalcode(String pa_postalcode) {
		this.pa_postalcode = pa_postalcode;
	}
	
	public String getPa_cityName() {
		return pa_cityName;
	}
	public void setPa_cityName(String pa_cityName) {
		this.pa_cityName = pa_cityName;
	}
	public String getPa_continent() {
		return pa_continent;
	}
	public void setPa_continent(String pa_continent) {
		this.pa_continent = pa_continent;
	}
	public String getPa_countryName() {
		return pa_countryName;
	}
	public void setPa_countryName(String pa_countryName) {
		this.pa_countryName = pa_countryName;
	}
	public String getPa_region() {
		return pa_region;
	}
	public void setPa_region(String pa_region) {
		this.pa_region = pa_region;
	}
	public String getPa_stateName() {
		return pa_stateName;
	}
	public void setPa_stateName(String pa_stateName) {
		this.pa_stateName = pa_stateName;
	}
	public int getAdminusertype() {
		return adminusertype;
	}
	public void setAdminusertype(int adminusertype) {
		this.adminusertype = adminusertype;
	}
	
	
	public String getRoleUuid() {
		return roleUuid;
	}
	public void setRoleUuid(String roleUuid) {
		this.roleUuid = roleUuid;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getP() {
		return p;
	}
	public void setP(int p) {
		this.p = p;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public List<String> getPageHtml() {
		return pageHtml;
	}
	public void setPageHtml(List<String> pageHtml) {
		this.pageHtml = pageHtml;
	}

}
